//
//  RWDetailPageHeaderView.m
//  RedWine
//
//  Created by Liufangfang on 2016/12/27.
//  Copyright © 2016年 HuiBangKe. All rights reserved.
//

#import "RWDetailPageHeaderView.h"

#define kTopScrollViewHeight 220.*FTY_HEIGHT_BASE
#define kDivideLineHeight 8.*FTY_HEIGHT_BASE
#define kMiddleViewHeight 67.*FTY_HEIGHT_BASE

#define kLineHeight .5*FTY_HEIGHT_BASE
#define kLeftEdge 10.*FTY_WIDTH_BASE
#define kRightEdge -10.*FTY_WIDTH_BASE

@interface RWDetailPageHeaderView ()<SDCycleScrollViewDelegate>

@property (nonatomic, copy) RWGoodsDetailInfoModel *infoModel;
@property (nonatomic, copy) NSArray *headerImages;

@end

@implementation RWDetailPageHeaderView

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [FTYColor lightGrayColor];
        [self setDetailPageHeaderUI];
    }
    return self;
}

- (void)loadDataWithModel:(RWGoodsDetailInfoModel *)infoModel{

    _infoModel = infoModel;
    _headerImages = infoModel.goods_image;
    [self configDatas];
}

- (void)configDatas{
    
    //现价属性化
    NSMutableAttributedString *moneyAttStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", _infoModel.goods_price]];
    NSDictionary *moneyAttDic = @{NSFontAttributeName: FTY_FONTTSIZE(22),
                                  NSForegroundColorAttributeName: [FTYColor getMainColorWithAlpha:1.]};
    [moneyAttStr addAttributes:moneyAttDic range:NSMakeRange(0, moneyAttStr.length)];
    //原价属性化
    NSMutableAttributedString *oldMoneyAttStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"原价: ¥ "]];
    NSAttributedString *oldMoneyNumAttStr = [[NSAttributedString alloc] initWithString:_infoModel.goods_marketprice attributes:@{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]}];
    [oldMoneyAttStr appendAttributedString:oldMoneyNumAttStr];

    [_scrollV setImagesGroup:_infoModel.goods_image];
    _titleLab.text = _infoModel.goods_name;
    _moneyLab.attributedText = moneyAttStr;
    _oldMoneyLab.attributedText = oldMoneyAttStr;
//    _productLab.text = _infoModel.goods_storage;
    _briefLab.text = _infoModel.jianjie;
}

- (void)setDetailPageHeaderUI{

    //====滚动视图
    SDCycleScrollView *scrollV = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, kTopScrollViewHeight)];
    [self addSubview:scrollV];
    scrollV.delegate = self;
    //cycleScrollView.autoScrollTimeInterval = 2.0;
    _scrollV = scrollV;
    
    //顶部线
//    UIView *sLine1 = [[UIView alloc] init];
//    [scrollV addSubview:sLine1];
//    sLine1.backgroundColor = [FTYColor grayColor];
//    [sLine1 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.left.right.mas_equalTo(0);
//        make.height.mas_equalTo(kLineHeight);
//    }];
    
    //底部线
    UIView *sLine2 = [[UIView alloc] init];
    [scrollV addSubview:sLine2];
    sLine2.backgroundColor = [FTYColor grayColor];
    [sLine2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0);
        make.height.mas_equalTo(kLineHeight);
    }];

    //====中间视图
    
    UIView *middleV = [[UIView alloc] init];
    [self addSubview:middleV];
    middleV.backgroundColor = [FTYColor whiteColor];
    [middleV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(scrollV.mas_bottom).offset(kDivideLineHeight);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(kMiddleViewHeight);
    }];
    
    //顶部线
    UIView *mLine1 = [[UIView alloc] init];
    [middleV addSubview:mLine1];
    mLine1.backgroundColor = [FTYColor grayColor];
    [mLine1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(kLineHeight);
    }];
    
    //底部线
    UIView *mLine2 = [[UIView alloc] init];
    [middleV addSubview:mLine2];
    mLine2.backgroundColor = [FTYColor grayColor];
    [mLine2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0);
        make.height.mas_equalTo(kLineHeight);
    }];
    
    UILabel *titleLab = [[UILabel alloc] init];
    [middleV addSubview:titleLab];
    titleLab.textColor = [FTYColor blackColor];
    titleLab.font = FTY_FONTTSIZE(14.);
    [titleLab sizeToFit];
    _titleLab = titleLab;
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(kLeftEdge);
        make.right.mas_equalTo(kRightEdge);
    }];
    
    //现价
    UILabel *moneyLab = [[UILabel alloc] init];
    [middleV addSubview:moneyLab];
    moneyLab.font = FTY_FONTTSIZE(13.);
    moneyLab.textColor = [FTYColor blackColor];
    _moneyLab = moneyLab;
    [moneyLab sizeToFit];
    [moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titleLab.mas_bottom).offset(10.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(titleLab);
    }];
    
    //原价
    UILabel *oldMoneyLab = [[UILabel alloc] init];
    [middleV addSubview:oldMoneyLab];
    oldMoneyLab.font = FTY_FONTTSIZE(13.);
    oldMoneyLab.textColor = [FTYColor blackColor];
    _oldMoneyLab = oldMoneyLab;
    [oldMoneyLab sizeToFit];
    [oldMoneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_moneyLab.mas_bottom);
        make.left.mas_equalTo(_moneyLab.mas_right).offset(10.*FTY_WIDTH_BASE);
    }];
    
    //产地
    UILabel *productLab = [[UILabel alloc] init];
    [middleV addSubview:productLab];
    productLab.font = FTY_FONTTSIZE(13.);
    productLab.textColor = [FTYColor blackColor];
    [productLab sizeToFit];
    [productLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_moneyLab.mas_bottom);
        make.left.mas_equalTo(oldMoneyLab.mas_right).offset(kLeftEdge);
        make.right.mas_lessThanOrEqualTo(kRightEdge);
    }];
    _productLab = productLab;
    //====底部视图
    
    UIView *bottomV = [[UIView alloc] init];
    [self addSubview:bottomV];
    bottomV.backgroundColor = [FTYColor whiteColor];
    [bottomV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(middleV.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(CGRectGetHeight(self.frame) - kTopScrollViewHeight - kDivideLineHeight - kMiddleViewHeight);
    }];
    
    //底部线
    UIView *bLine2 = [[UIView alloc] init];
    [bottomV addSubview:bLine2];
    bLine2.backgroundColor = [FTYColor grayColor];
    [bLine2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0);
        make.height.mas_equalTo(kLineHeight);
    }];
    
    UILabel *briefLab = [[UILabel alloc] init];
    [bottomV addSubview:briefLab];
    briefLab.textColor = [FTYColor blackColor];
    briefLab.font = FTY_FONTTSIZE(14);
    briefLab.numberOfLines = 0;
    _briefLab = briefLab;
    [briefLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(bottomV.mas_centerY);
        make.left.mas_equalTo(kLeftEdge);
        make.right.mas_offset(kRightEdge);
    }];
}

#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片：%@", index, _headerImages[index]);
}

@end
